package com.zfei.gfb.util;

import java.util.Vector;

public class IPTool {
    private static final int IPSEC = 256;
    private static final int MASKDEF = 0xFFFFFFFF;

    private Vector<IPEntity> list = new Vector<IPEntity>();

    public static void main(String[] args) {
        String url = "http://yxitv-test1.dg.163.org:8801/talk/listTalks";
        String urlMatchRe = ".*";
        boolean urlmatch = urlMatchRe != null && url.matches(urlMatchRe);
        System.out.println("url match test, url:" + url + ", urlregex: " + urlMatchRe + ", match:" + urlmatch);
        String internalIpFilterList = "10.160.215.121,127.0.0.1,10.241.24.103,192.168.149.163,192.168.149.113,10.255.252.59,10.120.104.0/24";
        IPTool util = new IPTool();
        String[] list = internalIpFilterList.split(",");
        for (String ipWithMask : list) {
            util.addIP(ipWithMask);
        }

        boolean allowed = util.contains("59.111.198.100");
        System.out.println(allowed);

    }

    public int size() {
        return list.size();
    }

    public void clear() {
        list.clear();
    }

    public int ip2int(String ip) {
        int value = 0;
        String i[] = ip.split("\\.");
        for (String t : i) {
            int v = Integer.parseInt(t);
            if (v < 0 || v > 255) {
                return 0;
            }
            value = value * IPSEC + v;
        }
        return value;
    }

    public int mask2int(String mask) {
        int imask = MASKDEF;
        int bits = 32 - Integer.parseInt(mask);
        if (bits < 0) {
            bits = 0;
        }
        imask = MASKDEF >> bits << bits;
        return imask;
    }

    public void addIP(String ip, String mask) {
        IPEntity e = new IPEntity(ip, mask);
        list.add(e);
    }

    public void addIP(String ipWithMask) {
        String[] t = ipWithMask.split("/");
        String ip = "0.0.0.0";
        String mask = "32";
        if (t.length == 2) {
            ip = t[0];
            mask = t[1];
        } else {
            ip = t[0];
        }
        addIP(ip, mask);
    }

    public void removeIP(String ip, String mask) {
        IPEntity e = new IPEntity(ip, mask);
        list.remove(e);
    }

    public boolean contains(String ip) {
        for (IPEntity e : list) {
            if (e.equals(ip)) {
                return true;
            }
        }
        return false;
    }

    private class IPEntity {
        private int iip = 0;
        private int imask = MASKDEF;

        IPEntity(String ip, String mask) {
            iip = ip2int(ip);
            imask = mask2int(mask);
        }

        public boolean equals(String target) {
            int targetIp = ip2int(target);
            return (iip == (targetIp & imask));
        }

        public boolean equals(Object obj) {
            if (obj instanceof IPEntity) {
                IPEntity e = (IPEntity) obj;
                return ((e.iip == this.iip) && (e.imask == this.imask));
            }
            return false;
        }
    }
}
